package export

import (
	"game/game-design-v.xnanz/loader"
	"strings"
)

type ExportArgs struct {
	SourceHome string //资源目录
	Mode       string //导出模式，fast快速导出，full完整导出
	ModeFile   string //用于快速导出时的文件MD5校验
	Exportfors []*ExportFor
	Reader     IDesignIO
	Writer     IDesignIO
}

// -exportfor csv D:\\Git\\codex001\\codex001-config\\export\\server-csv\\ useFileName server,server&cn
type ExportFor struct {
	Way          string
	OutputFolder string
	NameForm     string
	Tags         [][]string
}

func newExportFor(args []string) *ExportFor {
	var exportFor ExportFor
	exportFor.Way = args[0]
	exportFor.OutputFolder = args[1]
	exportFor.NameForm = args[2]
	tags := args[3]
	tagParts := strings.Split(tags, ",")
	for _, v := range tagParts {
		var taging []string
		parts := strings.Split(v, "&")
		taging = append(taging, parts...)
		exportFor.Tags = append(exportFor.Tags, taging)
	}

	return &exportFor
}

func NewExportArgs(args []string) *ExportArgs {
	var exportArgs ExportArgs
	for _, v := range args {
		parts := strings.Split(v, " ")
		if strings.HasPrefix(v, "-sourcehome") {
			exportArgs.SourceHome = parts[1]

		} else if strings.HasPrefix(v, "-exportfor") {
			values := make([]string, len(parts)-1)
			copy(values, parts[1:])
			exportFor := newExportFor(values)
			exportArgs.Exportfors = append(exportArgs.Exportfors, exportFor)

		} else if strings.HasPrefix(v, "-mode") {
			exportArgs.Mode = parts[1]
			exportArgs.ModeFile = parts[2]

		} else if strings.HasPrefix(v, "-reader") {
			exportArgs.Reader = GetDesignIO(parts[1])

		} else if strings.HasPrefix(v, "-writer") {
			exportArgs.Writer = GetDesignIO(parts[1])

		}
	}
	return &exportArgs
}

func GetDesignIO(ioName string) IDesignIO {
	if ioName == "excel" {
		return &ExcelIO{}
	} else if ioName == "csv" {
		return &CsvIO{}
	} else if ioName == "tjson" {
		return &TextJsonIO{}
	}
	return nil
}

type IDesignIO interface {
	Read(fileFullName string) ([]*loader.DesignFile, error)
	Write(designFile *loader.DesignFile, exportFor *ExportFor)
}
